package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.TeacherApplicationStatusEnums;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysAttendanceRecord;
import com.ruoyi.system.domain.SysSubject;
import com.ruoyi.system.domain.SysTeacher;
import com.ruoyi.system.mapper.SysAttendanceRecordMapper;
import com.ruoyi.system.service.ISysSubjectService;
import com.ruoyi.system.service.ISysTeacherService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSuperviseMapper;
import com.ruoyi.system.domain.SysSupervise;
import com.ruoyi.system.service.ISysSuperviseService;

/**
 * 辅导教师Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysSuperviseServiceImpl extends ServiceImpl<SysSuperviseMapper, SysSupervise> implements ISysSuperviseService
{
    @Autowired
    private SysSuperviseMapper sysSuperviseMapper;

    @Autowired
    private ISysUserService userService ;

    @Autowired
    private ISysTeacherService teacherService ;

    @Autowired
    private ISysSubjectService subjectService ;

    /**
     * 查询辅导教师
     * 
     * @param id 辅导教师主键
     * @return 辅导教师
     */
    @Override
    public SysSupervise selectSysSuperviseById(Long id)
    {
        return sysSuperviseMapper.selectSysSuperviseById(id);
    }

    /**
     * 查询辅导教师列表
     * 
     * @param sysSupervise 辅导教师
     * @return 辅导教师
     */
    @Override
    public List<SysSupervise> selectSysSuperviseList(SysSupervise sysSupervise)
    {
        if (SecurityUtils.hasRole("teacher")) {
            LambdaQueryWrapper<SysTeacher> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysTeacher::getUserId ,SecurityUtils.getUserId()) ;
            SysTeacher one = teacherService.getOne(wrapper);
            if (ObjectUtils.isNotEmpty(one)) {
                sysSupervise.setTeacherId(one.getId());
            }
        }
        if (SecurityUtils.hasRole("common")) {
            sysSupervise.setStudentId(SecurityUtils.getUserId());

        }

        List<SysSupervise> list = sysSuperviseMapper.selectSysSuperviseList(sysSupervise);
        for (SysSupervise supervise : list) {

            SysTeacher one = teacherService.getById(supervise.getTeacherId());
            if (ObjectUtils.isNotEmpty(one)) {
                SysSubject subject = subjectService.getById(one.getSubjectId());
                if (ObjectUtils.isNotEmpty(subject)) {
                    supervise.setSubjectName(subject.getName());
                }
                SysUser sysUser = userService.selectUserById(one.getUserId());
                supervise.setTeacher(sysUser);
            }

            supervise.setStudent(userService.selectUserById(supervise.getStudentId()));
        }
        return list;
    }

    /**
     * 新增辅导教师
     * 
     * @param sysSupervise 辅导教师
     * @return 结果
     */
    @Override
    public int insertSysSupervise(SysSupervise sysSupervise)
    {
        sysSupervise.setCreateTime(DateUtils.getNowDate());
        return sysSuperviseMapper.insertSysSupervise(sysSupervise);
    }

    /**
     * 修改辅导教师
     * 
     * @param sysSupervise 辅导教师
     * @return 结果
     */
    @Override
    public int updateSysSupervise(SysSupervise sysSupervise)
    {
        sysSupervise.setUpdateTime(DateUtils.getNowDate());
        return sysSuperviseMapper.updateSysSupervise(sysSupervise);
    }

    /**
     * 批量删除辅导教师
     * 
     * @param ids 需要删除的辅导教师主键
     * @return 结果
     */
    @Override
    public int deleteSysSuperviseByIds(Long[] ids)
    {
        return sysSuperviseMapper.deleteSysSuperviseByIds(ids);
    }

    /**
     * 删除辅导教师信息
     * 
     * @param id 辅导教师主键
     * @return 结果
     */
    @Override
    public int deleteSysSuperviseById(Long id)
    {
        return sysSuperviseMapper.deleteSysSuperviseById(id);
    }
}
